package com.example.administrator.base.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.administrator.base.baseActivity.BaseActivity;
import com.example.administrator.base.R;
import com.example.administrator.base.bean.ReLogin;
import com.example.administrator.base.util.GsonTools;
import com.example.administrator.base.util.SharedPreferencesUtils;
import java.io.IOException;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class Login extends BaseActivity {

    @InjectView(R.id.button)
    Button button;
    @InjectView(R.id.button5)
    Button button5;
    @InjectView(R.id.editText)
    EditText editText;
    @InjectView(R.id.editText2)
    EditText editText2;
    SweetAlertDialog sweetAlertDialog;
    @InjectView(R.id.button2)
    Button button2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.inject(this);


    }

    @Override
    protected void onResponseSuccess(Response response, String action) {
                        ResponseBody body = response.body();
                        if (body != null) {
                            try {
                               String  string = body.string();
                                Log.i("2125845",string);
                                final ReLogin mes = GsonTools.changeGsonToBean(string, ReLogin.class);
                                if (mes != null) {
                                    if (mes.getCode() == 1) {
                                        Log.i("33781",""+mes.getSystemCode());
                                        SharedPreferencesUtils.setParam(Login.this, "adminId", mes.getAdminId());
                                        SharedPreferencesUtils.setParam(Login.this, "administratorType", mes.getAdministratorType());
                                        SharedPreferencesUtils.setParam(Login.this, "areaId", mes.getAreaId());
                                        SharedPreferencesUtils.setParam(Login.this, "realName", mes.getRealName());
                                        SharedPreferencesUtils.setParam(Login.this, "cityNo", mes.getCityNo());
                                        SharedPreferencesUtils.setParam(Login.this, "sysCode", mes.getSystemCode());//判断是否从数据库调取车辆数据
                                        SharedPreferencesUtils.setParam(Login.this, "childrenAreaId", mes.getChildrenAreaId());
                                        SharedPreferencesUtils.setParam(Login.this, "position", mes.getPosition());
                                        if (mes.getParentSystemAreaName().equals("中华人民共和国")) {
                                            SharedPreferencesUtils.setParam(Login.this, "AreaName", mes.getSysAreaName());
                                        } else {
                                            if (mes.getSysAreaName().contains(mes.getParentSystemAreaName())) {
                                                SharedPreferencesUtils.setParam(Login.this, "AreaName", mes.getSysAreaName());
                                            } else {
                                                SharedPreferencesUtils.setParam(Login.this, "AreaName", mes.getParentSystemAreaName() + mes.getSysAreaName());
                                            }
                                        }
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                 showToast("登录成功");
                                                 sweetAlertDialog.dismissWithAnimation();
                                                 startActivity(new Intent(Login.this, MainActivity.class));
                                                 finish();
                                            }
                                        });
                                    } else {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                sweetAlertDialog.dismissWithAnimation();
                                                showToast(""+mes.getMessage());
                                            }
                                        });

                                    }
                                } else {
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            sweetAlertDialog.dismissWithAnimation();
                                            showToast("数据不完整，请稍后再试");
                                        }
                                    });
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                        }else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    sweetAlertDialog.dismissWithAnimation();
                                   showToast("数据不完整，请稍后再试");
                                }
                            });
                        }
    }

    @Override
    protected void onResponseFailure(Throwable e, String action) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                sweetAlertDialog.dismissWithAnimation();
              showToast("网络异常，请稍后再试");
            }
        });
    }

    @OnClick({R.id.button, R.id.button5})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.button:
                sweetAlertDialog = new SweetAlertDialog(this, 5);
                sweetAlertDialog.setCanceledOnTouchOutside(false);
                sweetAlertDialog.setTitleText("加载中");
                String phone = editText.getText().toString().trim();
                String password = editText2.getText().toString().trim();
                String cid = String.valueOf(SharedPreferencesUtils.getParam(Login.this, "clientid", ""));
                if (phone.equals("")) {
                    Toast.makeText(Login.this, "请输入手机号", Toast.LENGTH_LONG).show();
                    return;
                }
                if (password.equals("")) {
                    Toast.makeText(Login.this, "请输入密码", Toast.LENGTH_LONG).show();
                    return;
                }
                if (cid.equals("")) {
                    Toast.makeText(Login.this, "获取设备编号中。请稍后再试", Toast.LENGTH_LONG).show();
                    return;
                }
                sweetAlertDialog.show();
                logIn(phone, password, cid);

                break;
            case R.id.button5:
                sweetAlertDialog = new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE);
                sweetAlertDialog.setCanceledOnTouchOutside(true);
                sweetAlertDialog
                        .setTitleText("是否更换过手机号")
                        .setContentText("如果您的原手机已经弃用，无法收到验证码，请选择“是”")
                        .setConfirmText("是")
                        .setCancelText("否")
                        .showCancelButton(true)
                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sDialog) {
                                Intent intent = new Intent(Login.this, ResetPassword.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                intent.putExtra("havephone", "0");
                                startActivity(intent);
                                sDialog.dismiss();

                            }
                        })
                        .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                            @Override
                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                Intent intent = new Intent(Login.this, ResetPassword.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                intent.putExtra("havephone", "1");
                                startActivity(intent);
                                sweetAlertDialog.dismiss();
                            }
                        })
                        .show();
                break;
            default:
                break;
        }
    }

    public void logIn(String phone, String password, String cid) {
        RequestBody formBody = new FormBody.Builder()
                .add("action", "LOGIN")
                .add("phone", phone)
                .add("password", password)
                .add("adminCid", cid)
                .build();

        final Request request = new Request.Builder()
                .url("http://operate.99bicycle.com/OperateBicyclePro/administrator?")
                .post(formBody)
                .build();
        postRequest(request,"LOGIN");
    }

    @OnClick(R.id.button2)
    public void onViewClicked() {
        startActivity(new Intent(Login.this,Regiest.class));
    }
}
